﻿<%@ Page Language="C#" MasterPageFile="~/Manager.master" AutoEventWireup="true" EnableEventValidation="false"
    CodeFile="OrderApp.aspx.cs" Inherits="Manager_OrderApp_OrderApp" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

    <script language="javascript" type="text/javascript">
   
    var Url='<%= ResolveClientUrl("~/")%>';

    $(document).ready(function() {   
                    /*****/      
                           $(".clickRight").click(function () { 
                                $objL= '#<%= lbxLeft.ClientID%>';
                                $objR= '#<%= lbxRight.ClientID%>';
                                $($objL+" option[value='']").remove();
 
                                 if(fnHaveSelect($objL)==false) return false; 
                               if($($objL+' option:selected').size()<=0 )                      
                                {
                                    alert("Chọn sản phẩm") ;
                                    return ;
                                } 
                                $($objL+" option:selected").appendTo($objR);
                                $($objR+" option").removeAttr("selected"); 
                           });  
                  /*****/     
                           $(".clickleft").click(function () {        
                                $objR= '#<%= lbxRight.ClientID%>';       
                                $objL= '#<%= lbxLeft.ClientID%>';             
                                if(fnHaveSelect($objR)==false) return false;
                               
                               //Kiem tra neu objL==null
                                $($objL+" option[value='']").remove();
                                
                               $($objR+" option:selected").appendTo($objL);
                               $($objL+" option").removeAttr("selected");  
                           });  
                 /*****/         
                          $("#btnBasket").click(function () {
                                $objR= '#<%= lbxRight.ClientID%>';
                                if(fnHaveOptionList($objR)==false) return false;
                                
                               var ArrR = new Array();                              
                                    $($objR+' option').each(
                                         function(index, option)
                                           {                        
                                                ArrR[index] = option['value'];   
                                           }
                                    );
                                    GetAjaxAddBasket(ArrR);
                                
 
                           });             
                 /*****/          
                         $("#<%= txtTenSP.ClientID%>").keyup(function () {                                
                                GetAjaxLoadPDBasket();
                         });
                        $("#<%= ddlChuDe.ClientID%>").change(function () {                                
                                LoadCatalogue();                                
                          
                         });    
                         
                         $("#<%= ddlTheLoai.ClientID%>").change(function () {                                
                                GetAjaxLoadPDBasket();                                
                          
                         });     
                         $("#<%= ddlType.ClientID%>").change(function () {                                
                                GetAjaxLoadPDBasket();                                
                          
                         });                                   
                           
    });     
    
    function GetAjaxLoadPDBasket()
    {
         $txtTenSP='#<%= txtTenSP.ClientID%>';
         $objL= '#<%= lbxLeft.ClientID%>';
         $objR= '#<%= lbxRight.ClientID%>';      
         $($objL).html("");
         $($objL).append('<option value="">Loading...</option>');
         $ddlChuDe='<%= ddlChuDe.ClientID %>' ;
         $ddlTheLoai='<%= ddlTheLoai.ClientID %>';
         $ddlType='<%= ddlType.ClientID %>';
         var ArrR = new Array();     
         if($($objR+' option').size()>0 )
         {                                                                
                $($objR+' option').each(
                     function(index, option)
                       {                        
                            ArrR[index] = "'"+option['value']+"'";   
                       }
                );
         }
   
       $.ajax({ 
                               type: "GET", 
                               url: Url+"Pub/Ajax/AjaxQueryAdmin.aspx?type=OrderAppBasket&ddlTheLoai="+$('#'+$ddlTheLoai).val()+"&ddlChuDe="+$('#'+$ddlChuDe).val()+"&txtTenSP="+encodeURIComponent($($txtTenSP).val())+"&sExceptPDR="+ArrR+"&ddlType="+$('#'+$ddlType).val(), 
                               dataType: "json",
                               contentType: "application/json; charset=utf-8",
                               success: function(resp)
                               {   $($objL).html("");
                                    if(resp!=null || resp!=undefined) 
                                    {
                                            $options="";
                                            $.each(resp, function(key, val) {
                                          
                                                $.each(val, function(index, val1) { 
		                                           // $($objL).append(new Option(val1.PDID, val1.PDName));
		                                          $options+='<option value='+val1.PDID+' >'+val1.PDName+'</option>'; 
                                                });
                                             
                                            });
                                            $($objL).html($options);
                                            
                                     }
                                     else
                                     {
                                        if($($txtTenSP).val()!="") 
                                             $($objL).append('<option value="" style="color:red">không tìm thấy từ "<font style="color:blue">'+$($txtTenSP).val()+'</font>" </option>');
                                       else
                                                  $($objL).append('<option value="" style="color:red">không tìm thấy</option>');
                                     }
                                      
		                              
		                           
                               } 
               });        
    }
    
    function LoadCatalogue()
    {
        $ddlChuDe='#<%= ddlChuDe.ClientID%>';
         $ddlTheLoai='#<%= ddlTheLoai.ClientID%>';   
         $($ddlTheLoai).html('<option value="">Loading...</option>');
        $flagAjax=0;
           $.ajax({ 
                               type: "GET", 
                               url: Url+"Pub/Ajax/AjaxQueryAdmin.aspx?type=LoadCatalogueByTopic&TopicID="+$($ddlChuDe).val(), 
                               dataType: "json",
                               contentType: "application/json; charset=utf-8",
                               success: function(resp)
                               {      
                                    $($ddlTheLoai).html("");
                                   
                                       if(resp!=null || resp!=undefined) 
                                        {
                                                $options="";
                                                $.each(resp, function(key, val) {
                                              
                                                    $.each(val, function(index, val1) { 		                                           
		                                              $options+='<option value='+val1.CatID+' >'+val1.CatName+'</option>'; 
                                                    });
                                                 
                                                });
                                                $($ddlTheLoai).html('<option value="0">-- Chọn thể loại--</option>'+$options);
                                                $flagAjax=1;
                                                 if($flagAjax==1) 
                                                    GetAjaxLoadPDBasket(); 
                                             
                                                
                                         }
                                         else
                                         {
                                              $($ddlTheLoai).append('<option value="0">-- Chọn thể loại--</option>');
                                         } 
                                }  
          });       
         
                                                
    }
    
    
    function GetAjaxAddBasket(arr)
    {    
            var CodeID='<%= Request.QueryString["CodeID"]%>';
            
            $.ajax({ 
                                   type: "GET", 
                                   url: Url+"Pub/Ajax/AjaxQueryAdmin.aspx?type=AddBasket&ArrPD="+arr, 
                                   success: function(resp)
                                   { 
                                         if(resp=="ok")                                                      
                                         {
                                            window.location.href="BasketApp.aspx?CodeID=" +CodeID;
                                         }  
                                   } 
                            });        
    }
    
    function fnHaveSelect(obj)
    {
                if($(obj+' option:selected').size()<=0 )                      
                {
                    alert("Chọn sản phẩm") ;
                    return false;
                } 
        
    }
    function fnHaveOptionList(obj)
    {
                if($(obj+' option').size()<=0 )                      
                {
                    alert("Chọn sản phẩm") ;
                    return false;
                } 
        
    }
    
  


    </script>

    <table border="0" cellpadding="2" cellspacing="0" style="width: 100%">
        <tr>
            <td colspan="3">
                <table border="1" bordercolor="#407db3" cellpadding="2" cellspacing="2" class="tbl_Comon-Search"
                    style="margin-left: 0px; width: 100%; border-collapse: collapse" width="100%">
                    <tr>
                        <td align="center" nowrap="nowrap" rowspan="2" style="width: 4%; display: none" valign="middle"
                            visible="false">
                            <asp:Button ID="btnSearch" runat="server" CssClass="cssButton" OnClick="btnSearch_Click"
                                Text="Tìm kiếm" Width="80px" /></td>
                        <td class="td_Title" nowrap="nowrap" style="width: 10%; white-space: nowrap">
                            <asp:Label ID="Label1" runat="server" CssClass="lbl_Common-Search" Text="Tên sản phẩm"></asp:Label></td>
                        <td class="td_Control" nowrap="nowrap">
                            <asp:TextBox ID="txtTenSP" runat="server" CssClass="txt_Product" Height="20px" Width="98%"></asp:TextBox></td>
                        <td class="td_Title" nowrap="nowrap" style="width: 10%; white-space: nowrap; display: none;">
                            <asp:Label ID="Label2" runat="server" CssClass="lbl_Common-Search" Text="Chủ đề"></asp:Label></td>
                        <td class="td_Control" nowrap="nowrap" style="display: none">
                            <asp:DropDownList ID="ddlChuDe" runat="server">
                            </asp:DropDownList></td>
                    </tr>
                    <tr>
                        <td class="td_Title" nowrap="nowrap" style="width: 10%; white-space: nowrap">
                            Loại</td>
                        <td class="td_Control" nowrap="nowrap">
                            <asp:DropDownList ID="ddlType" runat="server">
                                <asp:ListItem Value="P" Selected="True">Sản phẩm</asp:ListItem>
                                <asp:ListItem Value="F">Film</asp:ListItem>
                            </asp:DropDownList></td>
                        <td class="td_Title" nowrap="nowrap" style="width: 10%; white-space: nowrap; display: none;">
                            <asp:Label ID="Label3" runat="server" CssClass="lbl_Common-Search" Text="Thể loại"></asp:Label></td>
                        <td class="td_Control" nowrap="nowrap" style="display: none">
                            <asp:DropDownList ID="ddlTheLoai" runat="server">
                            </asp:DropDownList></td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td align="right" colspan="3">
                <input id="btnBasket" type="button" value="Cập nhật giỏ hàng" class="cssButton" />
                <asp:Button ID="btnCancelBasket" runat="server" CssClass="cssButton" Text="Hủy bỏ giỏ hàng"
                    OnClick="btnCancelBasket_Click" /></td>
        </tr>
        <tr>
            <td align="center" style="width: 50%; background-color: #33ccff" valign="middle">
                <asp:Label ID="Label5" runat="server" Font-Bold="True" Text="Kho sản phẩm"></asp:Label></td>
            <td align="center" rowspan="2" style="width: 19px; background-color: snow">
                <img src="../../Images/Icon/right.jpg" class="clickRight" /><br />
                <br />
                <img src="../../Images/Icon/left.jpg" class="clickleft" /><br />
                <br />
                <br />
            </td>
            <td align="center" style="width: 50%; background-color: mediumturquoise" valign="middle">
                <asp:Label ID="Label6" runat="server" Font-Bold="True" Text="Giỏ hàng"></asp:Label></td>
        </tr>
        <tr>
            <td align="right" style="width: 50%; background-color: #33ccff;" valign="middle">
                <asp:ListBox ID="lbxLeft" runat="server" Height="400px" Width="100%" SelectionMode="Multiple"
                    EnableTheming="True" Style="border-top-style: none; border-right-style: none;
                    border-left-style: none; border-bottom-style: none"></asp:ListBox></td>
            <td align="left" style="width: 50%; background-color: mediumturquoise;" valign="middle">
                <asp:ListBox ID="lbxRight" runat="server" Height="400px" Width="100%" SelectionMode="Multiple"
                    Style="border-top-style: none; border-right-style: none; border-left-style: none;
                    border-bottom-style: none; background-image: url('../../Images/Body/basketadminbg.png');
                    background-repeat: no-repeat; background-position: center"></asp:ListBox></td>
        </tr>
        <tr>
            <td>
            </td>
            <td style="width: 19px">
            </td>
            <td>
            </td>
        </tr>
    </table>

    <script language="javascript" type="text/javascript">
    function fnLoadData()
    {   
        var txtTenSP=document.getElementById("<%= txtTenSP.ClientID %>");
       
        var lbxLeft=document.getElementById("<%=lbxLeft.ClientID%>");
        lbxLeft.options.length=0;
        var newOpt = new Option();
         newOpt.text = "Loading...";
         newOpt.value="";
         //lbxLeft.options.add(opt);
         lbxLeft.options[lbxLeft.options.length]=newOpt
        
        
        var ddlChuDe=document.getElementById("<%= ddlChuDe.ClientID %>");   
      
        var ddlTheLoai=document.getElementById("<%= ddlTheLoai.ClientID %>"); 
        var lbxRight=document.getElementById("<%=lbxRight.ClientID%>");
        
        var sExceptPDR="";
        if(lbxRight.options.length>0)
        {
            for(var i=0;i<lbxRight.options.length;i++)
            {
                sExceptPDR += "'" + lbxRight.options[i].value + "',";
            }
            sExceptPDR = sExceptPDR.substr(0, sExceptPDR.length - 1);       
        }
        Manager_OrderApp_OrderApp.AjaxGetPD(txtTenSP.value,ddlChuDe.value,ddlTheLoai.value,sExceptPDR,CallBack_LoadData)
    }
    function CallBack_LoadData(res)
    {
        if(res!=null)
        {
            var ds=res.value;
            if(ds!=null && ds!="")
			{	
			    var lbxLeft=document.getElementById("<%=lbxLeft.ClientID%>");			    
		         lbxLeft.options.length=0;
		        if(ds.Tables[0].Rows.length<=0)
		        {
		             var newOpt = new Option();
		             newOpt.text = ">>>>> Not Found Item! <<<<<";
                     newOpt.value="";
                     lbxLeft.options[lbxLeft.options.length]=newOpt 
                    return; 
                      
		        }  
			    for(var i=0;i<ds.Tables[0].Rows.length;i++)
			    {
			        var value=ds.Tables[0].Rows[i].PdID;
		            var text=ds.Tables[0].Rows[i].PdName;
		            
                     var newOpt = new Option();
		             newOpt.text = text;
                     newOpt.value=value;
                     //lbxLeft.options.add(opt);
                     lbxLeft.options[lbxLeft.options.length]=newOpt
			        //lbxLeft.options[i].value=value;
			        //lbxLeft.options[i].text=text;
			    }
			}	
        }
    }
    function fnOrderApp()
    {
        var width=900;
        var height=600;
        var left = parseInt((screen.availWidth/2) - (width/2));
		var top = parseInt((screen.availHeight/2) - (height/2));
        //var strFeatures="dialogWidth="+width+",dialogHeight="+height+",menu=0,scrollbars=1,status=1,location=0,resizable=1,left=" + left + ",top="+top+",screenX=" + left + ",screenY=" + top;
        strFeatures = "dialogWidth="+width+"px;dialogHeight="+height+"px;center=yes;help=no;status=yes;resizable=yes";
           var sPath = "OrderDetailApp.aspx";
	       var  strDate = showModalDialog(sPath,"_blank",strFeatures);
    }
    </script>

</asp:Content>
